package com.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.mapper.UserMapper;
import com.common.model.User;
import com.common.query.UserQueryParam;
import com.common.service.UserService;
import com.util.CombineBeansUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    @Override
    public User findByUserId(long id) {
        return userMapper.selectById(id);
    }

    @Override
    public User findByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);

        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public List<User> findByUsernameLike(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", username);

        return userMapper.selectList(queryWrapper);
    }

    @Override
    public int save(User user) {

        user.setRegisterTime(String.valueOf(System.currentTimeMillis()));

        return userMapper.insert(user);
    }

    @Override
    public void edit(User user) {
        userMapper.updateById(user);
    }

    @Override
    public boolean edit(Long id, User user) {

        User oldUser = userMapper.selectById(id);

        if (oldUser == null) {
            return false;
        } else {
            try {
                CombineBeansUtil.combineBean(oldUser, user);
            } catch (InvocationTargetException | IllegalAccessException e) {
                e.printStackTrace();
            }
            userMapper.updateById(user);
        }

        return true;
    }


    @Override
    public void delete(long id) {
        userMapper.deleteById(id);
    }

    @Override
    public IPage<User> findAll(Integer page, Integer pageSize) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true, false, "create_time");

        return userMapper.selectPage(new Page<User>(page, pageSize), queryWrapper);
    }

    @Override
    public IPage<User> findAll(Integer page, Integer pageSize, UserQueryParam userQueryParam) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true, false, "user_id");

        if (userQueryParam.getUsername() != null) {
            queryWrapper.like("username", userQueryParam.getUsername());
        }

        if (userQueryParam.getNickName() != null) {
            queryWrapper.like("nick_name", userQueryParam.getNickName());
        }

        if (userQueryParam.getEmail() != null) {
            queryWrapper.like("email", userQueryParam.getEmail());
        }

        return userMapper.selectPage(new Page<>(page, pageSize), queryWrapper);
    }
}